<?php
class Db
{
    static private $_instance;
    static private $_connectSource;

    // 数据库配置
    private $_dbConfig = [
        'host' => '127.0.0.1',
        'user' => 'root',
        'password' => '',
        'database' => '17003334',
    ];

    // 示例化对象
    static public function getInstance()
    {
        //进行判断，如果已经实例化过，就不会再重新实例化了，用instanceof self 进行判断
        if (!(self::$_instance instanceof self)) {
            //注意是如何实例化的
            self::$_instance = new self();
        }
        return self::$_instance;
    }

    // 连接
    public function connect()
    {
        if (!self::$_connectSource) {
            //返回的是一个资源句柄
            self::$_connectSource = mysqli_connect($this->_dbConfig['host'], $this->_dbConfig['user'], $this->_dbConfig['password']);

            //选择一个数据库操作
            mysqli_select_db(self::$_connectSource, $this->_dbConfig['database']);

            //设置字符集
            mysqli_set_charset(self::$_connectSource, "UTF8");
        }
        return self::$_connectSource;
    }
}
